package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"logi-serve/cache"
	"logi-serve/model"
	"strconv"
	"time"
)

func PersonAdd(c *gin.Context) {
	person_name := c.PostForm("person_name")
	person_address := c.PostForm("person_address")
	person_phone, _ := strconv.Atoi(c.PostForm("person_phone"))
	data := model.Person{
		PersonName:    person_name,
		PersonAddress: person_address,
		PersonPhone:   person_phone,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func PersonOrderAdd(c *gin.Context) {
	order_name := c.PostForm("order_name")
	pid, _ := strconv.Atoi(c.PostForm("pid"))
	order_statue := c.PostForm("order_statue")

	data := model.PersonOrder{
		OrderName:   order_name,
		Pid:         pid,
		OrderStatue: order_statue,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func PersonOrderList(c *gin.Context) {
	/*
		SELECT
		  person_orders.id as OrderId,
		  person_orders.order_name as OrderName,
		  person_orders.order_statue as OrderStatue,
		  people.person_name as PersonName,
		  people.person_address as PersonAddress,
		  people.person_phone as PersonPhone
		FROM
		  person_orders
		  LEFT JOIN people ON person_orders.pid = people.id
	*/
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	var total int64
	model.DB.Model(&model.PersonOrder{}).Count(&total)
	var data []model.PersonOrderList
	redis_key := "order_list_" + strconv.Itoa(int(page))
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("person_orders").
			Select(
				"person_orders.id as OrderId",
				"person_orders.order_name as OrderName",
				"person_orders.order_statue as OrderStatue",
				"people.person_name as PersonName",
				"people.person_address as PersonAddress",
				"people.person_phone as PersonPhone",
			).
			Joins("LEFT JOIN people ON person_orders.pid = people.id").
			Offset(offset).Limit(size).Debug().Find(&data)
		jsonData, _ := json.Marshal(data)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &data)
	}
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "订单查询成功",
		"total": total,
		"data":  data,
	})
	return
}
func PersonOrderDetail(c *gin.Context) {
	/*
		SELECT
		  person_orders.id as OrderId,
		  person_orders.order_name as OrderName,
		  person_orders.order_statue as OrderStatue,
		  people.person_name as PersonName,
		  people.person_address as PersonAddress,
		  people.person_phone as PersonPhone
		FROM
		  person_orders
		  LEFT JOIN people ON person_orders.pid = people.id
		  WHERE person_orders.id=1
	*/
	id := c.Query("id")
	var detail []model.PersonOrderList
	redis_key := "order_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("person_orders").
			Select(
				"person_orders.id as OrderId",
				"person_orders.order_name as OrderName",
				"person_orders.order_statue as OrderStatue",
				"people.person_name as PersonName",
				"people.person_address as PersonAddress",
				"people.person_phone as PersonPhone",
			).
			Where("person_orders.id=?", id).
			Joins("LEFT JOIN people ON person_orders.pid = people.id").
			Debug().Find(&detail)
		jsonData, _ := json.Marshal(detail)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &detail)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "订单查询成功",
		"data": detail,
	})
	return
}
